const query = require('../config/sql');
const { v4: uuidv4 } = require('uuid');

module.exports = async function createagreement(agreement) {
    const sql = `select approvaler_id,applicant_id,petId from approvallist where approval_id="${agreement.id}"`
    await query(sql).then(async (result) => {   
        let sql2=`select nickname,name,avatar from cardlist where card_id="${result[0].petId}"`
        if (result) {
            await query(sql2).then(async res=>{
                let agreement_id = uuidv4();
                let sql3 = `insert into agreement(agreement_id,sender_id, pet_id, status,adapt_id,approval_id,adaptername,petname,avatar) values(?,?,?,?,?,?,?,?,?);`
                if (res) {
                    await query(sql3,[agreement_id,result[0].applicant_id,result[0].petId,0,result[0].approvaler_id,agreement.id,res[0].nickname,res[0].name,res[0].avatar])
                }
            })
        }
    })

}